package com.example.home_stretch;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MActivity extends AppCompatActivity {
    @BindView(R.id.gv_sp)
    GridView gvSp;
    private List<String> Name = new ArrayList<>();
    //这是视频的储存路径，创建一个raw目录，里面的视频文件叫ndhw.mp4
    private int[] Mp4=new int[]{R.raw.ndhw,R.raw.ndhw,R.raw.ndhw,R.raw.ndhw};
    //这是获取手机储存权限的代码
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_m);
        ButterKnife.bind(this);
        Name.add("视频名称");
        Name.add("视频名称一");
        Name.add("视频名称二");
        Name.add("视频名称三");
        LitePal.getDatabase();
        verifyStoragePermissions();
        initview();
    }
    private void verifyStoragePermissions() {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(MActivity.this,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(MActivity.this, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private class MyPlay implements MediaPlayer.OnCompletionListener {
        @Override
        public void onCompletion(MediaPlayer mp) {
            Toast.makeText(MActivity.this, "视频播放结束", Toast.LENGTH_SHORT).show();
        }
    }
    private void initview() {
        gvSp.setAdapter(new gAdapter());
    }

    class gAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return Name.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup parent) {
            View view1 = View.inflate(MActivity.this, R.layout.activiy_mone, null);
            ViewHolder viewHolder=new ViewHolder(view1);
            viewHolder.tvSpname.setText(Name.get(i));
            //设置视频控制器
            viewHolder.vvSp.setMediaController(new MediaController(MActivity.this));
            //设置视频路径
            viewHolder.vvSp.setVideoURI(Uri.parse("android.resource://com.example.home_stretch/"+Mp4[i]));
            //播放完回调
            viewHolder.vvSp.setOnCompletionListener(new MyPlay());
            viewHolder.tvSpname.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewHolder.vvSp.start();
                }
            });
            return view1;
        }
        class ViewHolder {
            @BindView(R.id.vv_sp)
            VideoView vvSp;
            @BindView(R.id.tv_spname)
            TextView tvSpname;
            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
